import * as actionTypes from "./actionTypes";
import axios from "axios";
import { fromJS } from "immutable";

const changeList = data => ({
    type: actionTypes.CHANGE_LIST,
    data: fromJS(data),
    totalPage: Math.ceil(data.length / 10)
});

const searchFocus = () => ({
    type: actionTypes.SEARCH_FOCUS
});

const searchBlur = () => ({
    type: actionTypes.SEARCH_BLUR
});

const mouseEnter = () => ({
    type: actionTypes.MOUSE_ENTER
});

const mouseLeave = () => ({
    type: actionTypes.MOUSE_LEAVE
});

const changePage = page => ({
    type: actionTypes.CHANGE_PAGE,
    page
});

const getList = () => {
    return dispatch => {
        const url = "/api/headerList.json";
        axios
            .get(url)
            .then(res => {
                const data = res.data.data;
                dispatch(changeList(data));
            })
            .catch(err => {
                console.error(err);
            });
    };
};

export { searchFocus, searchBlur, mouseEnter, mouseLeave, changePage, getList };
